import numpy
from flask import jsonify, request
from flask.blueprints import Blueprint

from config import DEMO, CH_TO_EN_MAP, SCORE_MAP
from errors import BaseError
from services.evidences import Evidence
from services.coefficients import Coefficient

evidence_bp = Blueprint('evidence', __name__)


@evidence_bp.route('/evidences/')
def get_evidence():
    keywords = request.args.get('keywords')
    # 预留从算法服务获取数据列表
    # evidence_list = Evidence.get_evidence(keywords)
    evidence_list = DEMO.get(keywords)
    if not evidence_list:
        raise BaseError()
    return evidence_list


@evidence_bp.route('/scores/', methods=["POST"])
def get_score():
    print("-------------")
    request_data = request.json
    print(request_data)
    evidence_list = request_data.get('evidence_list')
    # TODO 将前端数据转化成矩阵
    line = len(evidence_list)
    # TODO 获取系数，与矩阵相乘得到对应的结果
    try:
        coefficient = Coefficient.get_ach_coefficient(line)
        ach_score = Coefficient.ACH_to_score(evidence_list)
        print(coefficient)
        print(ach_score)
        res = Coefficient.calculation_socre(ach_score, coefficient)[0]
    except Exception as e:
        print(e)
        raise BaseError()
    return list(res)
